from django.shortcuts import render, redirect
from app01 import models
from app01.utils.pagination import Pagination
from app01.utils.form import UserModelForm


def user_list(request):
    """用户管理"""
    # [对象,对象,...]
    user_info = models.UserInfo.objects.all()

    """
    # python语法处理
    for obj in user_info:
        print(
            obj.id,
            obj.name,
            obj.age,
            obj.account,
            obj.creat_time.strftime("%Y-%m_%d"),
            obj.get_gender_display(),
            obj.depart_id,
            obj.depart.title
        )
    """
    """
    obj.gender -> (1,2)
    obj.get_gender_display -> ('男', '女')

    obj.depart_id -> 外键id
    obj.depart.title -> id对应的值
    """
    page_obj = Pagination(request, user_info, page_size=3)
    context = {
        'queryset': page_obj.page_queryset,
        'page_str': page_obj.html()
    }

    return render(request, 'user_list.html', context)


def user_update(request):
    """添加用户(原始方法)"""
    if request.method == 'GET':
        context = {
            'gender_choice': models.UserInfo.gender_cho,
            'dept': models.Department.objects.all()
        }
        return render(request, 'user_update.html', context)
    user = request.POST.get('user')
    pwd = request.POST.get('pwd')
    age = request.POST.get('age')
    account = request.POST.get('ac')
    ctime = request.POST.get('ctime')
    gender = request.POST.get('gd')
    depart_id = request.POST.get('dp')

    # 添加到数据库中
    models.UserInfo.objects.create(name=user,
                                   password=pwd,
                                   age=age,
                                   account=account,
                                   creat_time=ctime,
                                   gender=gender,
                                   depart_id=depart_id)
    # 返回用户列表
    return redirect('/user/list/')


def user_m_update(request):
    """添加用户modelform"""
    if request.method == 'GET':
        form = UserModelForm()
        return render(request, 'user_m_update.html', {'form': form})
    # POST 提交数据, 数据校验
    form = UserModelForm(data=request.POST)
    if form.is_valid():
        # print(form.cleaned_data)
        # 数据合法则保存到数据库
        form.save()
        return redirect('/user/list/')

    return render(request, 'user_m_update.html', {'form': form})


def user_edit(request, nid):
    """编辑用户"""
    # 根据nid在数据库中获取数据(对象)
    row_obj = models.UserInfo.objects.filter(id=nid).first()
    if request.method == 'GET':
        # instance=obj, 会讲obj的所有值显示在输入框中
        form = UserModelForm(instance=row_obj)
        return render(request, 'user_edit.html', {'form': form})

    # 进行编辑时, 需要将编辑对象进行定位
    # 校验数据, 并保存到row_obj所在行
    form = UserModelForm(data=request.POST, instance=row_obj)
    if form.is_valid():
        # 默认保存的是用户输入的所有数据; 如果想要再用户输入以外在增加值
        # form.instance.字段名 = 值
        form.save()
        return redirect('/user/list/')
    return render(request, 'user_edit.html', {'form': form})


def user_delete(request, nid):
    """删除用户"""
    models.UserInfo.objects.filter(id=nid).delete()
    return redirect('/user/list/')
